/**
 * @file restart_and_reset.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_RESTART_AND_RESET != 0

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/

/**********************
 *  STATIC VARIABLES
 **********************/
#if USE_POWER_OFF_GIF
static lv_task_t * restart_task_p;
#endif

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
lv_obj_t * restart_and_reset_create(lv_obj_t * activity_obj)
{
    lv_obj_t * obj;
    lv_obj_t * label;
    lv_obj_t * btn_restart;
    lv_obj_t * btn_reset;
    lv_obj_t * img_restart;
    lv_obj_t * img_reset;
    lv_obj_t * label_restart;
    lv_obj_t * label_reset;

    if(NULL == activity_obj) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_RESTART_AND_RESET;
        activity_ext.create = restart_and_reset_create;
        activity_ext.prepare_destory = restart_and_reset_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(NULL == activity_obj) {
            return NULL;
        }
    }

    obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(NULL == obj) {
        return NULL;
    }

    label = lv_label_create(obj, NULL);
    lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font20_gray);
    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
    lv_obj_set_click(label, false);
    lv_label_set_text_id(label, WATCH_TEXT_ID_RESTART_AND_RESET);
    lv_obj_align(label, obj, LV_ALIGN_IN_TOP_MID, 0, 10);

    btn_restart = lv_btn_create(obj, NULL);
    lv_obj_add_style(btn_restart, LV_BTN_PART_MAIN, &lv_style_transp_fit);
    lv_btn_set_fit(btn_restart, LV_FIT_TIGHT);
    lv_btn_set_layout(btn_restart, LV_LAYOUT_ROW_MID);
    lv_obj_set_event_cb(btn_restart, restart_btn_action);
    lv_watch_obj_add_element(btn_restart);

    img_restart = lv_img_create(btn_restart, NULL);
    lv_img_set_src(img_restart, ICON_SETTING_REBOOT);

    label_restart = lv_label_create(btn_restart, NULL);
    lv_obj_add_style(label_restart, LV_LABEL_PART_MAIN, &lv_watch_font20);

    lv_label_set_text_id(label_restart, WATCH_TEXT_ID_RESTART);

    btn_reset = lv_btn_create(obj, btn_restart);
    lv_obj_set_event_cb(btn_reset, reset_btn_action);
    lv_watch_obj_add_element(btn_reset);

    img_reset = lv_img_create(btn_reset, NULL);
    lv_img_set_src(img_reset, ICON_SETTING_RESTORE);

    label_reset = lv_label_create(btn_reset, NULL);
    lv_obj_add_style(label_reset, LV_LABEL_PART_MAIN, &lv_watch_font20);

    lv_label_set_text_id(label_reset, WATCH_TEXT_ID_FACTORY_RESET);
    lv_label_set_long_mode(label_reset, LV_LABEL_LONG_BREAK);
    lv_obj_set_width(label_reset, LV_HOR_RES * 2 / 3);

    if(lv_obj_get_base_dir(obj) == LV_BIDI_DIR_RTL){
        lv_obj_align(btn_restart, obj, LV_ALIGN_CENTER, 20, -20);
        lv_obj_align(btn_reset, btn_restart, LV_ALIGN_OUT_BOTTOM_RIGHT, 0, 10);
    } else {
        lv_obj_align(btn_restart, obj, LV_ALIGN_CENTER, -20, -20);
        lv_obj_align(btn_reset, btn_restart, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 10);
    }

    return obj;
}

void restart_and_reset_create_btn_action(lv_obj_t * btn, lv_event_t event)
{
    (void)btn;

    if(LV_EVENT_CLICKED != event) return;

    lv_watch_png_cache_all_free();

    lv_obj_t * obj = restart_and_reset_create(NULL);
    LV_ASSERT_MEM(obj);
    if(obj == NULL) return;
}

lv_obj_t * restart_create(lv_obj_t * activity_obj)
{
    lv_obj_t * obj;
    lv_obj_t * label;
    lv_obj_t * label_tip;
    lv_obj_t * content;
    lv_obj_t * btn_no;
    lv_obj_t * btn_yes;

    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_RESTART;
        activity_ext.create = restart_create;
        activity_ext.prepare_destory = restart_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(NULL == activity_obj) {
            return NULL;
        }
    }

    obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(NULL == obj) {
        return NULL;
    }

    label = lv_label_create(obj, NULL);
    lv_obj_set_size(label, LV_HOR_RES, LV_VER_RES / 6);
    lv_label_set_text_id(label, WATCH_TEXT_ID_RESTART);
    lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font20_gray);
    lv_obj_align(label, obj, LV_ALIGN_IN_TOP_MID, 0, 25);

    content = lv_cont_create(obj, NULL);
    lv_obj_add_style(content, LV_CONT_PART_MAIN, &lv_watch_style_transp);
    lv_cont_set_fit(content, LV_FIT_TIGHT);
    lv_cont_set_layout(content, LV_LAYOUT_CENTER);
    LV_ASSERT_MEM(content);
    if(NULL == content) {
        return NULL;
    }
    lv_obj_align(content, label, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
    lv_obj_set_click(content, false);
    lv_watch_obj_add_element(content);

    label_tip = lv_label_create(content, NULL);
    lv_label_set_text_id(label_tip, WATCH_TEXT_ID_RESTART_Q);
    lv_obj_add_style(label_tip, LV_LABEL_PART_MAIN, &lv_watch_font20);

    btn_no = lv_imgbtn_create(obj, NULL);
    lv_imgbtn_set_src(btn_no, LV_BTN_STATE_RELEASED, ICON_CANCEL);
    lv_imgbtn_set_src(btn_no, LV_BTN_STATE_PRESSED, ICON_CANCEL);
    lv_obj_align(btn_no, NULL, LV_ALIGN_IN_BOTTOM_LEFT, 30, -20);
    lv_obj_set_event_cb(btn_no, restart_no_btn_action);
    lv_watch_obj_add_element(btn_no);

    btn_yes = lv_imgbtn_create(obj, NULL);
    lv_imgbtn_set_src(btn_yes, LV_BTN_STATE_RELEASED, ICON_OK);
    lv_imgbtn_set_src(btn_yes, LV_BTN_STATE_PRESSED, ICON_OK);
    lv_obj_align(btn_yes, NULL, LV_ALIGN_IN_BOTTOM_RIGHT, -30, -20);
    lv_obj_set_event_cb(btn_yes, restart_yes_btn_action);
    lv_watch_obj_add_element(btn_yes);

    return obj;
}

void restart_btn_action(lv_obj_t * btn, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;
    restart_create(NULL);
}

void reset_btn_action(lv_obj_t * btn, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;
    factorymode_reset_create(NULL);
}

void restart_and_reset_prepare_destory(lv_obj_t * activity_obj)
{
    lv_watch_png_cache_all_free();
}

void restart_prepare_destory(lv_obj_t * activity_obj)
{
    lv_watch_png_cache_all_free();
}

#if USE_POWER_OFF_GIF
static void restart_task(lv_task_t * task)
{
    gif_info_t * info;
    info = (gif_info_t *)task->user_data;

    if(info->once_done) {
        gif_close(info);
        Hal_Backlight_Off();
        Hal_Tone_Play_End();
        Hal_Power_Off(HAL_TYPE_REBOOT);
        lv_task_del(restart_task_p);
        printf("restart ongoing.\n");
    }
}
#endif

uint8_t play_anim_power_off(void)
{
    /* shutdown */
    watch_set_ready_state(false);
    watch_set_suspend_enable(false, ACT_ID_RESTART, 0);
#if USE_POWER_OFF_GIF
    shutdown_music();

    lv_obj_t * activity_obj = NULL;
    lv_watch_activity_ext_t activity_ext;
    memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
    activity_ext.actId = ACT_ID_ANYOBJ;
    activity_obj = lv_watch_creat_activity_obj(&activity_ext);
    LV_ASSERT_MEM(activity_obj);
    if(activity_obj == NULL) return 0;

    gif_info_t * gif_info;
    gif_info = gif_open(activity_obj, ANIM_POWER_OFF, 1);
    if(!gif_info) {
        printf("shutdown gif failed.\n");
        return 0;
    }
    restart_task_p = lv_task_create(restart_task, 100, LV_TASK_PRIO_MID, (void *)gif_info);
#else
    printf("restart without power off gif.\n");
    Hal_Backlight_Off();
    Hal_Power_Off(HAL_TYPE_REBOOT);
#endif
    return 1;
}

void restart_yes_btn_action(lv_obj_t * btn, lv_event_t event)
{
    if(LV_EVENT_CLICKED != event) return;

    lv_obj_clean(lv_obj_get_parent(btn));
#if USE_LV_BLUETOOTH != 0
    bt_power_off();
#endif
    dial_poweroff();
    play_anim_power_off();
}

void restart_no_btn_action(lv_obj_t * btn, lv_event_t event)
{
    lv_obj_t * act_obj;

    if(LV_EVENT_CLICKED != event) return;

    act_obj = lv_watch_get_activity_obj(ACT_ID_RESTART);
    lv_obj_del(act_obj);
}

/**********************
 *   STATIC FUNCTIONS
 **********************/

#endif /*USE_LV_WATCH_RESTART_AND_RESET*/
